package com.th.admin.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * @author ThreePure
 * @date 2022/4/21 17:14
 * @description: TODO
 * @since 1.8
 */
@Deprecated
//@Configuration
public class DataSourceConfig {

    /*@Bean
    @ConfigurationProperties("spring.datasource")*/  //复用配置文件的数据源配置
    public DataSource createDataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        //添加监控功能
        druidDataSource.setFilters("stat,wall");
//        druidDataSource.setUrl();   使用配置文件中 spring.datasource.url代替
//        druidDataSource.setUsername();
//        druidDataSource.setPassword();
        return druidDataSource;
    }

    /**
     * 配置 druid的监控页功能
     */
    /*@Bean*/
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();

        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<StatViewServlet>(statViewServlet,"/druid/*");
        //监控页账号密码：
        registrationBean.addInitParameter("loginUsername","admin");
        registrationBean.addInitParameter("loginPassword","123456");
        return registrationBean;
    }


    /**
     * WebStatFilter 用于采集web-jdbc关联监控的数据。
     */
    /*@Bean*/
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();

        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        //排除一些不必要的url请求
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,*.woff,/druid/*");

        return filterRegistrationBean;
    }
}
